package one.bugu.android.demon.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.DecimalFormat;

import one.bugu.android.demon.R;

public class DialogStrike extends Dialog {

    private Context mContext;
    private ImageView iv_stri_cancle;
    private TextView tv_stri_value;
    private TextView tv_stri_btn;


    public DialogStrike(@NonNull Context context) {
        super(context);
        this.mContext = context;
        initView();
    }

    public DialogStrike(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
        initView();
    }

    protected DialogStrike(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
        initView();
    }

    private void initView() {
        View view = View.inflate(mContext, R.layout.dialog_strike, null);
        setContentView(view);
        iv_stri_cancle = view.findViewById(R.id.iv_stri_cancle);
        tv_stri_value = view.findViewById(R.id.tv_stri_value);
        tv_stri_btn = view.findViewById(R.id.tv_stri_btn);

        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.TOP);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        WindowManager wm = this.getWindow().getWindowManager();
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        lp.width = width - 100;
        lp.y = (int) (height * 0.2);
        dialogWindow.setAttributes(lp);
        this.setCanceledOnTouchOutside(false);

        tv_stri_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onShareBtnClick != null) {
                    onShareBtnClick.onShareBtnClickListener();
                }
            }
        });
        iv_stri_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onShareBtnClick != null) {
                    onShareBtnClick.onShareCancleListener();
                }
            }
        });
    }

    private OnShareBtnClick onShareBtnClick;

    public interface OnShareBtnClick {
        void onShareBtnClickListener();

        void onShareCancleListener();
    }

    public void setOnShareBtnClick(OnShareBtnClick onShareBtnClick) {
        this.onShareBtnClick = onShareBtnClick;
    }

    @SuppressLint("SetTextI18n")
    public void setStriValue(double value) {
        String format = new DecimalFormat("###").format(value * 100);
        tv_stri_value.setText(format + "%");
    }
}
